package com.foreveross.fetl.application.impl;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dayatang.querychannel.support.Page;
import com.foreveross.fetl.application.ILogMessageApplication;
import com.foreveross.fetl.common.application.BaseApplicationImpl;
import com.foreveross.fetl.core.domain.freight.LogMessage;
import com.foreveross.fetl.core.domain.freight.repository.ILogMessageRepository;
import com.foreveross.fetl.vo.LogMessageVO;

@Transactional
@Service("logMessageApplication")
public class LogMessageApplicationImpl extends BaseApplicationImpl implements
		ILogMessageApplication {
	
	@Inject
	private ILogMessageRepository logMessageRepository;

	@Override
	public void create(LogMessageVO logMessageVO) {
		LogMessage entity =this.voToEntity(logMessageVO);
		logMessageRepository.create(entity);

	}

	@Override
	public void update(LogMessageVO logMessageVO) {
		LogMessage entity =this.voToEntity(logMessageVO);
		logMessageRepository.update(entity);
	}

	@Override
	public LogMessageVO findByID(Long id) {
		LogMessage entity =logMessageRepository.get(LogMessage.class, id);
		LogMessageVO vo =this.entityToVO(entity);
		return vo;
	}

	@Override
	public Page<LogMessageVO> pageQueryLog(LogMessageVO logvo, int currentPage,
			int pageSize) {
		List<LogMessageVO> result =new ArrayList<LogMessageVO>();
		List<Object> conditionVals = new ArrayList<Object>();
		StringBuilder jpql =new StringBuilder("select logMes from LogMessage logMes where 1=1 ");
		if(logvo.getExecuteClass()!=null && !"".equals(logvo.getExecuteClass())){
			jpql.append(" and logMes.executeClass like ? ");
			conditionVals.add(MessageFormat.format("%{0}%",logvo.getExecuteClass()));
		}
		if(logvo.getExecuteName()!=null && !"".equals(logvo.getExecuteName())){
			jpql.append(" and logMes.executeName = ? ");
			conditionVals.add(logvo.getExecuteName());
		}
		if(logvo.getExecuteType()!=0){
			jpql.append(" and logMes.executeType = ? ");
			conditionVals.add(logvo.getExecuteType());
		}
		
		if (logvo.getExecuteStartTime() != null) {
	   		jpql.append(" and logMes.executeStartTime between ? and ? ");
	   		conditionVals.add(logvo.getExecuteStartTime());
	   		conditionVals.add(logvo.getExecuteTimeEnd());
	   	}
		jpql.append(" order by logMes.executeStartTime desc ");
		 Page<LogMessage> pages = this.queryChannel1().queryPagedResultByPageNo(jpql.toString(), conditionVals.toArray(), currentPage, pageSize);
		 for (LogMessage logMessage : pages.getResult()) {
			 LogMessageVO vo = new LogMessageVO();
			 vo =this.entityToVO(logMessage);
			 result.add(vo);
		 }
		return new Page<LogMessageVO>(pages.getCurrentPageNo(), pages.getTotalCount(), pages.getPageSize(), result);
	}
	
	private LogMessageVO entityToVO(LogMessage entity){
		LogMessageVO vo= new LogMessageVO();
		vo.setId(entity.getId());
		vo.setExecuteClass(entity.getExecuteClass());
		vo.setExecuteName(entity.getExecuteName());
		vo.setExecuteStartTime(entity.getExecuteStartTime());
		vo.setExecuteType(entity.getExecuteType());
		vo.setResult(entity.getResult());
		vo.setRowKey(entity.getRowKey());
		vo.setTasksNumber(entity.getTasksNumber());
		vo.setFailuresNumber(entity.getFailuresNumber());
		vo.setInsertNumber(entity.getInsertNumber());
		vo.setExecuteTotalTime(entity.getExecuteTotalTime());
		vo.setExecuteEndTime(entity.getExecuteEndTime());
		vo.setTaskTime(entity.getTaskTime());
		return vo;
	}
	
	private LogMessage voToEntity(LogMessageVO vo){
		LogMessage entity =new LogMessage();
		entity.setId(vo.getId());
		entity.setExecuteClass(vo.getExecuteClass());
		entity.setExecuteName(vo.getExecuteName());
		entity.setExecuteStartTime(vo.getExecuteStartTime());
		entity.setExecuteType(vo.getExecuteType());
		entity.setResult(vo.getResult());
		entity.setRowKey(vo.getRowKey());
		entity.setTasksNumber(vo.getTasksNumber());
		entity.setFailuresNumber(vo.getFailuresNumber());
		entity.setInsertNumber(vo.getInsertNumber());
		entity.setExecuteTotalTime(vo.getExecuteTotalTime());
		entity.setExecuteEndTime(vo.getExecuteEndTime());
		entity.setTaskTime(vo.getTaskTime());
		return entity;
	}

	@Override
	public void delete(Long id) {
		logMessageRepository.delete(LogMessage.class, id);
	}

	@Override
	public LogMessageVO getOneLogMessage(String executeClass,
			String executeName, Date executeStartTime, int executeType) {
		LogMessage entity =logMessageRepository.getOneLogMessage(executeClass, executeName, executeStartTime, executeType);
		if(null!=entity){
			LogMessageVO vo =this.entityToVO(entity);
			return vo;
		}
		return null;
	}

}
